# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from fetchGit.items import RepertoryZipedItem
from fetchGit.settings import savePath
from os import path
from shutil import os
from io import BytesIO
from zipfile import ZipFile


class FetchgitPipeline(object):

    def process_item(self, item, spider):
        if isinstance(item, RepertoryZipedItem):
            p = path.join(savePath, item['auth'])
            if not path.isdir(p):
                os.makedirs(p)
            data = BytesIO(item['data'])
            ZipFile(data, 'r').extractall(p)
